﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Asion.Dapper
{
    public class SimpleMultipleCrudRepository<TEntity> : BaseRepository, IMultipleCrudRepository<TEntity>
    {

        public Tuple<ResultA, ResultB> QueryMultiple<ResultA, ResultB>(string sql, object? param = null)
        {
            ResultA resultA = default(ResultA);
            ResultB resultB = default(ResultB);
            using (var connection = GetDbConnection<TEntity>())
            {
                var girdReaderResult = connection.QueryMultiple(sql, param);
                resultA = girdReaderResult.ReadFirst<ResultA>();
                resultB = girdReaderResult.ReadFirst<ResultB>();
            }
            var result = Tuple.Create(resultA, resultB);
            return result;

        }

        public Tuple<ResultA, ResultB, ResultC> QueryMultiple<ResultA, ResultB, ResultC>(string sql, object? param = null)
        {
            ResultA resultA = default(ResultA);
            ResultB resultB = default(ResultB);
            ResultC resultC = default(ResultC);
            using (var connection = GetDbConnection<TEntity>())
            {
                var girdReaderResult = connection.QueryMultiple(sql, param);
                 resultA = girdReaderResult.ReadFirst<ResultA>();
                 resultB = girdReaderResult.ReadFirst<ResultB>();
                 resultC = girdReaderResult.ReadFirst<ResultC>();               
            }
            var result = Tuple.Create(resultA, resultB, resultC);
            return result;
        }

        public Tuple<ResultA, ResultB, ResultC, ResultD> QueryMultiple<ResultA, ResultB, ResultC, ResultD>(string sql, object? param = null)
        {
            ResultA resultA = default;
            ResultB resultB = default(ResultB);
            ResultC resultC = default(ResultC);
            ResultD resultD = default(ResultD);
            using (var connection = GetDbConnection<TEntity>())
            {
                var girdReaderResult = connection.QueryMultiple(sql, param);
                 resultA = girdReaderResult.ReadFirst<ResultA>();
                 resultB = girdReaderResult.ReadFirst<ResultB>();
                 resultC = girdReaderResult.ReadFirst<ResultC>();
                 resultD = girdReaderResult.ReadFirst<ResultD>();               
            }
            var result = Tuple.Create(resultA, resultB, resultC, resultD);
            return result;
        }
    }
}
